package net.osweb.core.tools.utils.excel;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import net.osweb.core.tools.exception.ExcelParseException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;



public class ExcelParseUtil {
	private static Log log = LogFactory.getLog(ExcelParseUtil.class);
	@SuppressWarnings("unchecked")
	public static List parseExcel(File file, int column)throws ExcelParseException {
		InputStream is = null;
		Workbook rwb = null;
		boolean blankFlag = false;		
		int killRow = 0;
		List resultList = new ArrayList();
		try {
			is = new FileInputStream(file);
			rwb = Workbook.getWorkbook(is);
			Sheet st[] = rwb.getSheets();
			for (int a = 0; a < 1; a++) {
				log.debug(st[a].getRows());
				
				for (int i = 0; i < st[a].getRows(); i++) {
					int killColumn = 0;
					List rowList = new ArrayList();
					log.debug(st[a].getColumns());
					for (int j = 0; j < column; j++) {						
						Cell c00 = st[a].getCell(j, i);
						// 閫氱敤鐨勮幏鍙朿ell鍊肩殑鏂瑰紡,杩斿洖�?�楃涓�?
						log.info(c00.getContents().trim());
						if(!"".equals(c00.getContents().trim())){
							blankFlag = true;							
						}else{
							killColumn ++;
						}						
						log.debug(killColumn);	
						rowList.add(j, c00.getContents().trim());						
					}
					if(killColumn == column ){
						killRow ++;
					}
					
					if(killRow >= 5){
						break;
					}
					//褰撹В鏋愬嚭鏉ョ殑涓�琛屾暟鎹叏閮戒�?  "" 鐨勬椂鍊欙紝杩囨护姝よ鏁版�?
					if(blankFlag){
						resultList.add(rowList);
						blankFlag = false;
					}
				}
			}
			// 鍏抽�?
		} catch (Exception e) {
			log.error(e);
			throw new ExcelParseException(e);
		} finally {
			try {
				if(is != null){
					is.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			if(rwb != null)
			rwb.close();
		}
		return resultList;
	}
}
